<?php
/**
 * Created by PhpStorm.
 * Author: sitenv@aliyun.com
 * CreateTime: 2023/8/17 06:39
 * Blog：www.myblogs.xyz
 */

namespace app\domain\repository;

use app\infrastructure\enum\OrderEnum;
use app\infrastructure\model\OrderModel;
use app\domain\entity\OrderEntity;
use app\domain\service\OrderDomainService;

class OrderRepositoryImpl
{
    public function save(OrderEntity $order): bool
    {
        $param = [
            'user_id'=>$order->getUserId(),
            'goods_id'=>1,
            'shop_id'=>1,
            'order_code'=>order_code(),
            'goods_name'=>'test goods name',
            'amount'=>$order->getAmount(),
            'create_time'=>NEW_TIME,
            'update_time'=>NEW_TIME,
            'status'=>OrderEnum::NOT_PAY
        ];
        $order = new OrderModel();
        return $order->insert($param);
    }

    public function getById($id): array
    {
        $order = new OrderModel();
        $fields = '';
        return $order->getFind($id, $fields);
    }

    // 获取订单列表
    public static function getList(array $where, string $limit): array
    {
        $order = new OrderModel();
        // or 从缓存中读取数据
        return $order->getList($where, $limit, '', ['user_id,goods_id,shop_id', true]);
    }

    // 订单详情
    public static function getShow(array $where): array
    {
        $order = new OrderModel();
        $fields = '';
        // or 从缓存中读取数据
        $orderData = $order->getFind($where, $fields);
        $orderData['discount'] = OrderDomainService::integralDiscount(
            $orderData['user_id'],
            $orderData['amount']
        );
        return $orderData;
    }
}